//
//  GestionCoupon.m
//  Truffle
//
//  Created by Ismaila Kredity on 07/02/10.
//  Copyright 2010 kredity. All rights reserved.
//

#import "GestionCoupon.h"
#import "ExchangeCoupons.h"



//Permet de savoir si une cellule a ete selectionne
BOOL selectedC = FALSE;
//permet de connaitre les informations de la cellule selectionnee
NSIndexPath * selectedCIndexPathc; 
//cette variable permet de savoir si on charge la vue une prmiere fois
BOOL firstViewC = FALSE;
//cette variable permet de savoir si la meme cellule est selectionne
BOOL sameCellC = FALSE;


@implementation GestionCoupon

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 }
 return self;
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"Coupons";
	
	listSections = [[NSArray alloc] initWithObjects:@"section1", nil]; 
	listOfSectionOne = [[NSArray alloc] initWithObjects:@"Coupon 1", @"coupon 2", @"coupon 3", @"coupon 4", nil];
}


/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}



// added by raca - BEGIN
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	switch (section) {
		case(0):
			return @"My coupons";
			break;
		default :
			break;
	}
	return nil;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
	[tableView reloadData];
	
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tblView {
	return [listSections count];
}

- (NSInteger)tableView:(UITableView *)tblView numberOfRowsInSection:(NSInteger)section {
	switch (section) {
		case 0:
			return [listOfSectionOne count];
			break;
		case 1:
			return [listOfSectionTwo count];
			break;
		default:
			break;
	}
	return -1;
}

- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"Cell";
	UILabel * typeRef;
	
	UITableViewCell *cell = [tblView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
		//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
	}
	
	
	int nbviews = [[cell subviews] count];
	UIFont *displayFont = [UIFont boldSystemFontOfSize:12];
	
	[cell addSubview:[[UILabel alloc] initWithFrame:CGRectMake(200, 15, 70, 20)]];
	typeRef = [[cell subviews] objectAtIndex:nbviews];
	//NSString * libelle = [list objectAtIndex:indexPath.section];
	
	NSString * ref = @"";
	
	int idx = indexPath.row;
	//switch sur les sections du UITableView
	switch (indexPath.section) {
		case 0:
			cell.textLabel.text =[listOfSectionOne objectAtIndex:idx];
			break;
		case 1:
			cell.textLabel.text =[listOfSectionTwo objectAtIndex:idx];
			break;
		default:
			break;
	}
	
	cell.imageView.image = nil;
	//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
	cell.accessoryView = nil;
	typeRef.text = ref;
	typeRef.font = displayFont;
	
	return cell;
}



- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if(indexPath.row >= 0)
	{
		if(exchangeCoupons == nil)
			exchangeCoupons = [[ExchangeCoupons alloc] initWithNibName:@"ExchangeCoupons" bundle:nil];
		[self.navigationController pushViewController:exchangeCoupons animated:YES];		
	}
	
	selectedC = TRUE;
	
	if(selectedCIndexPathc.section == indexPath.section && !firstViewC)
	{
		firstViewC = TRUE;
		sameCellC = TRUE;
	} else {
		firstViewC = FALSE;
		sameCellC = FALSE;
	}
	
	selectedCIndexPathc = indexPath;
	
	[tableView reloadData];
	
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
		//Get the object to delete from the array.
		NSString * message = @"Suppression";
		NSString * messageText = @"Suppression de ce coupon de ma liste des favoris";
		
		UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:message
							  message:[NSString stringWithFormat:@"%@", messageText]
							  delegate:nil
							  cancelButtonTitle:nil
							  //otherButtonTitles:@"OK",  nil];
							  otherButtonTitles:@"OK",  nil];
		[alert show];
	}   
}


// added by raca - END

- (void)dealloc {
    [super dealloc];
}


@end
